//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.DataLayer.ServiceModel
{
    using System;
    using System.Globalization;

    /// <summary>
    /// Respresents a workflow for a UI element
    /// </summary>
    [Serializable]
    public class WorkflowModel : BaseModel
    {
        private DateTime modifiedTime;
        private string versionLabel;

        /// <summary>
        /// Initializes a new instance of the <see cref="WorkflowModel"/> class.
        /// </summary>
        public WorkflowModel()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="WorkflowModel"/> class.
        /// </summary>
        /// <param name="workflow">The workflow.</param>
        public WorkflowModel(Activity workflow)
            : base(workflow)
        {
            this.VersionLabel = workflow.VersionLabel;
        }

        /// <summary>
        /// Gets or sets the version label.
        /// </summary>
        /// <value>The version label.</value>
        public string VersionLabel
        {
            get
            {
                return versionLabel;
            }
            private set
            {
                versionLabel = value;
            }
        }

        /// <summary>
        /// The modified time of the workflow.
        /// </summary>
        public DateTime ModifiedTime
        {
            get
            {
                return modifiedTime;
            }
            private set
            {
                modifiedTime = value;
            }
        }

        /// <summary>
        /// Gets or sets the label.
        /// </summary>
        /// <value>The label.</value>
        public override string Label
        {
            get
            {
                return string.IsNullOrEmpty(this.VersionLabel) ? this.Name : string.Format(CultureInfo.CurrentUICulture, "{0} ({1})", this.Name, this.VersionLabel);
            }
            set
            {
                base.Label = value;
            }
        }

        /// <summary>
        /// Sorts the activity list based on workflow name and the modified time.
        /// Implementation of Icomparer object
        /// </summary>
        /// <param name="activityA">Activity</param>
        /// <param name="activityB">Activity</param>
        /// <returns></returns>
        public static int WorkFlowComparer(WorkflowModel first, WorkflowModel second)
        {
            int result = 0;
            string firstStringToCompare = first.Name;
            string secondStringToCompare = second.Name;
            int compareValue = string.Compare(firstStringToCompare, secondStringToCompare, StringComparison.OrdinalIgnoreCase);
            if (compareValue != 0)
            {
                result = compareValue > 0 ? 1 : -1;
            }
            else
            {
                compareValue = DateTime.Compare(first.ModifiedTime, second.ModifiedTime);
                result = compareValue > 0 ? -1 : 1;
            }
            return result;
        }
    }
}
